﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using System;

public class LobbyPanel : UIBase
{
    private static string path = "UI/LobbyPanel";
    private static UILayer layer = UILayer.Bottom;

    private Dictionary<int, FuncCard> functionList;
    private Button headBtn;
    private Transform functionListTrs;

    protected override void Awake()
    {
        base.Awake();
        functionList = new Dictionary<int, FuncCard>()
        {
            {1,new FuncCard("Sprite/fightcardicon","竞技场","",null) },
            {2,new FuncCard("Sprite/preparecardicon","战备","",null) },
            {3,new FuncCard("Sprite/shopcardicon","商店","",()=>OpenUI<ShopPanel>(UIPanel.ShopPanel,UIPanel.LobbyPanel)) }
        };
        functionListTrs = transform.Find("FunctionList");
        headBtn = transform.Find("MainHead/Border").GetComponent<Button>();
        headBtn.AddClickListener(() => OpenUI<PlayerInfoBox>(UIPanel.PlayerInfoBox));
    }

    public override void Open(params System.Object[] parameters)
    {
        SetFunctionList();
    }

    public override void Close()
    {
        DestroyImmediate(this.gameObject);
    }

    private void SetFunctionList()
    {
        Loader.Instance.LoadAsyn<Transform>("UI/FuncCard", (trs) =>
        {
            float startX = Screen.width / 2f + trs.GetComponent<RectTransform>().sizeDelta.x / 2f;
            float posX = -Screen.width / 2 + Screen.width / 3f / 2f;
            Transform temp = null;
            Sequence seq = DOTween.Sequence().Pause();
            using (var e=functionList.GetEnumerator())
            {
                while (e.MoveNext())
                {
                    temp = Instantiate<Transform>(trs);
                    temp.SetParent(functionListTrs);
                    temp.Reset();
                    temp.localPosition = new Vector3(startX, 0, 0);
                    seq.Append(temp.DOLocalMoveX(posX, 0.3f));
                    seq.AppendInterval(0.05f);
                    posX += Screen.width / 3f;
                    SetFunctionItem(temp, e.Current.Value);
                }
            }
            seq.Play();
        });
    }
    
    private void SetFunctionItem(Transform trs,FuncCard card)
    {
        Button detailBtn = trs.Find("DetailBtn").GetComponent<Button>();
        Text title = trs.Find("Title").GetComponentInChildren<Text>();
        Image cardIcon = trs.Find("CardIcon").GetComponent<Image>();
        detailBtn.AddClickListener(() => OpenUI<DetailBox>(UIPanel.DetailBox, card.funcTitle + "规则", card.funcDetail));
        title.text = card.funcTitle;
        Loader.Instance.LoadAsyn<Sprite>(card.cardIconName, (sprite) =>
        {
            Vector2 size = sprite.rect.size;
            cardIcon.rectTransform.sizeDelta = size;
            cardIcon.sprite = sprite;
        });
        trs.GetComponent<Button>().AddClickListener(()=>
        {
            card.panelOpenDelegate?.Invoke();
        });
    }
}

public class FuncCard
{
    public string cardIconName;
    public string funcTitle;
    public string funcDetail;
    public VoidDelegate panelOpenDelegate;

    private FuncCard()
    {

    }
    public FuncCard(string _cardName,string _funcTitle,string _funcDetail,VoidDelegate _delegate)
    {
        this.cardIconName = _cardName;
        this.funcTitle = _funcTitle;
        this.funcDetail = _funcDetail;
        this.panelOpenDelegate = _delegate;
    }
}
